package org.adream.account.service;

import java.util.List;

import org.adream.account.dao.LoginStateDao;
import org.adream.account.entity.LoginStateEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.model.UserLoginStateExceptionModel;
import org.adream.account.util.Constant;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class LoginStateService {
	
	@Autowired
	private LoginStateDao loginStateDao;
	
	private final static Logger logger = LoggerFactory.getLogger(LoginStateService.class);
	
	/**
	 * 增
	 * @param loginState
	 * @return
	 */
	public ResultModel<?> addLoginState(LoginStateEntity loginState) {
		loginState.setWrongTimes(Constant.ONE);
		loginState.setDr(Constant.ONE);
		int result = loginStateDao.addLoginState(loginState);
		if(result == Constant.ZERO) {
			logger.warn("增加用户登录状态失败,数据库异常:(");
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误213,请联系管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, "增加成功", null);
	}
	
	/**
	 * 删除
	 * @param uid
	 * @return
	 */
	public ResultModel<?> deleteLoginState(String uid) {
		LoginStateEntity loginState = loginStateDao.queryLoginStateByUid(uid);
		if(loginState == null) {
			logger.warn("删除用户登录状态失败,数据不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "删除失败,数据不存在", null);
		}
		int result = loginStateDao.deleteLoginState(uid);
		if(result == Constant.ZERO) {
			logger.warn("删除用户登录状态失败,数据库异常:(");
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误214,请联系管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, "更新成功", null);
	}
	
	/**
	 * 更新登录错误次数
	 * @param loginStateEntity
	 * @return
	 */
	public ResultModel<?> updateWrongTimes(LoginStateEntity loginStateEntity) {
		LoginStateEntity loginState = loginStateDao.queryLoginStateByUid(loginStateEntity.getUid());
		if(loginState == null) {
			logger.warn("更新用户登录状态失败,数据不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "更新失败,数据不存在", null);
		}
		loginState.setWrongTimes(loginStateEntity.getWrongTimes());
		int result = loginStateDao.updateWrongTimes(loginState);
		if(result == Constant.ZERO) {
			logger.warn("更新用户登录状态失败,数据库异常:(");
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误215,请联系管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, "更新成功", null);
	}
	
	/**
	 * 查
	 * @param uid
	 * @return
	 */
	public ResultModel<?> queryLoginState(String uid) {
		LoginStateEntity loginState = loginStateDao.queryLoginStateByUid(uid);
		if(loginState == null) {
			logger.warn("查询用户登录状态失败,数据不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, "查询成功", null);
	}
	
	/**
	 * 查询超过最大登录次数的登录异常名单
	 * @param dr
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public ResultModel<?> queryUserLoginStateExceptions(String uid, Integer dr, Integer pageNo, Integer pageSize) {
		PageHelper.startPage(pageNo, pageSize);
		List<UserLoginStateExceptionModel> models = loginStateDao.queryUserLoginStateExceptions(uid, dr, Constant.MAX_WRONG_TIMES);
		if(Utils.isEmptyCollection(models)) {
			logger.warn("查询登录异常名单失败,数据不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		//封装Page对象 每页显示10条记录
		PageInfo<UserLoginStateExceptionModel> page = new PageInfo<>(models, Constant.TEN);
		return new ResultModel<PageInfo<UserLoginStateExceptionModel>>(ResultModel.SUCCESS, "查询成功", page);
	}
}